package com.pri.chat.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.LookMemberListBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.adapter.LookMemberAdapter;
import com.pri.chat.base.BaseActivity;
import com.pri.chat.utils.Des3Util;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author
 * @time 2020/5/9
 */
public class SearchMemberActivity extends BaseActivity implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.rec_main)
    RecyclerView recMain;
    @BindView(R.id.none)
    ImageView none;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    @BindView(R.id.et_search)
    EditText et_search;
    @BindView(R.id.searchBtn)
    TextView searchBtn;



    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_member_layout;
    }

    @Override
    protected void initView() {
        setTitle("搜索用户");
        initLayout();
        initSrl();


    }

    @Override
    protected void initData() {

    }

    private List<LookMemberListBean> mList;
    private LookMemberAdapter mAdapter;
    private int page = 1;
    private boolean isShowDialog = false;

    private void initLayout() {
        mList = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recMain.setLayoutManager(linearLayoutManager);
        mAdapter = new LookMemberAdapter(R.layout.item_look_member, mList);
        recMain.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    private void searchData() {
        SubscriberOnNextListener<BaseBean<List<LookMemberListBean>>> onNextListener = baseBean -> {
            if (baseBean.getData().isEmpty() && page == 1) {
                none.setVisibility(View.VISIBLE);
                mAdapter.notifyDataSetChanged();
                RxToast.normal(getString(R.string.no_data));
            } else {
                none.setVisibility(View.INVISIBLE);
                mList.addAll(baseBean.getData());
                mAdapter.notifyDataSetChanged();
            }
        };
        Map<String,Object> dtModel = new HashMap<>();//登录上传参数
        dtModel.put("memberId",et_search.getText().toString());
        dtModel.put("pageIndex",page);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(dtModel));
        HttpMethods.getInstance().getSearchMeMember(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }

    private void initSrl() {
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh_layout.finishRefresh(1000);
            refresh();
        });
        srlAll.setOnLoadmoreListener(refresh_layout -> {
            refresh_layout.finishLoadmore(1000);
            isShowDialog = false;
            page++;
            searchData();
        });
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Bundle bundle =new Bundle();
        bundle.putString("id",mList.get(position).getId());
        RxActivityTool.skipActivity(this, OtherPeopleMsgActivity.class,bundle);
    }

    private void refresh() {
        page = 1;
        isShowDialog = false;
        mList.clear();
        mAdapter.notifyDataSetChanged();
        searchData();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        Bundle bundle =new Bundle();
        bundle.putString("id",mList.get(position).getId());
        RxActivityTool.skipActivity(this, OtherPeopleMsgActivity.class,bundle);
    }

    @OnClick({R.id.searchBtn})
    public void onClick(View v){
        if(v.getId() == R.id.searchBtn){
            if(et_search.getText().toString().equals("")){
                RxToast.showToast("请输入搜索内容");
                return;
            }
            refresh();
        }
    }
}
